package com.tjvib.util;

import static com.tjvib.Constants.APPINFO_NAME;
import static com.tjvib.Constants.LPMSB2_SENSOR_NAME;
import static com.tjvib.Constants.TOKEN_NAME;
import static com.tjvib.Constants.ZEROLAB_SENSOR_NAME;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 用户账户信息保存工具类
 */
public class SharedPreferencesUtil {

    /**
     * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
     *
     * @param context
     * @param key
     * @param object
     */
    private static void setParam(Context context, String FILE_NAME, String key, Object object) {

        String type = object.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();

        if ("String".equals(type)) {
            editor.putString(key, (String) object);
        } else if ("Integer".equals(type)) {
            editor.putInt(key, (Integer) object);
        } else if ("Boolean".equals(type)) {
            editor.putBoolean(key, (Boolean) object);
        } else if ("Float".equals(type)) {
            editor.putFloat(key, (Float) object);
        } else if ("Long".equals(type)) {
            editor.putLong(key, (Long) object);
        }
        editor.commit();
    }

    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     *
     * @param context
     * @param key
     * @param defaultObject
     * @return
     */
    private static Object getParam(Context context, String FILE_NAME, String key, Object defaultObject) {
        String type = defaultObject.getClass().getSimpleName();
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);

        if ("String".equals(type)) {
            return sp.getString(key, (String) defaultObject);
        } else if ("Integer".equals(type)) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if ("Boolean".equals(type)) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if ("Float".equals(type)) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if ("Long".equals(type)) {
            return sp.getLong(key, (Long) defaultObject);
        }

        return null;
    }

    /**
     * 清除所有数据
     *
     * @param context
     */
    private static void clearAll(Context context, String FILE_NAME) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear().commit();
    }

    /**
     * 清除指定数据
     *
     * @param context
     */
    private static void clearAll(Context context, String FILE_NAME, String key) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    public static void setInitialized(Context context, boolean value) {
        setParam(context, APPINFO_NAME, "initialized", value);
    }

    public static boolean getInitialized(Context context) {
        return (boolean) getParam(context, APPINFO_NAME, "initialized", false);
    }

    public static void saveLpmsB2SensorName(Context context, String address, String name) {
        setParam(context, LPMSB2_SENSOR_NAME, address, name);
    }

    public static void saveZeroLabSensorName(Context context, String address, String name) {
        setParam(context, ZEROLAB_SENSOR_NAME, address, name);
    }

    public static String getLpmsb2SensorName(Context context, String address) {
        return (String) getParam(context, LPMSB2_SENSOR_NAME, address, "未知设备");
    }

    public static String getZeroLabSensorName(Context context, String address) {
        return (String) getParam(context, ZEROLAB_SENSOR_NAME, address, "未知设备");
    }

    public static void saveToken(Context context, String token) {
        setParam(context, TOKEN_NAME, "qwerty", token);
    }

    public static void clearToken(Context context) {
        setParam(context, TOKEN_NAME, "qwerty", "");
    }

    public static String getToken(Context context) {
        return (String) getParam(context, TOKEN_NAME, "qwerty", "");
    }
}